package com.zhuiyun.project.api.dict.service.impl;

import com.zhuiyun.project.api.dict.entity.DictData;
import com.zhuiyun.project.api.dict.entity.DictType;
import com.zhuiyun.project.api.dict.mapper.DictDataMapper;
import com.zhuiyun.project.api.dict.model.DictDataModel;
import com.zhuiyun.project.api.dict.service.DictDataService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.util.BeanUtils;
import com.zhuiyun.project.util.ObjectUtils;
import com.zhuiyun.project.util.RedisUtil;
import com.zhuiyun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.util.pageUtil.PageRequest;
import com.zhuiyun.project.util.pageUtil.PageResult;
import com.zhuiyun.project.util.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Access;
import java.util.Date;
import java.util.List;

/**
 * @ClassName DictDataServiceImpl
 * @Description TODO
 * @Author Root
 * @Date 2023/6/22 11:26
 **/
@Service
public class DictDataServiceImpl implements DictDataService {
    @Autowired
    DictDataMapper dictDataMapper;

    /**
    * @Author Root
    * @Description 查询字典信息
    * @Date 2023/7/24 19:27
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param dictDataModel
    */
    @Override
    public CommonResult updateDictData(DictDataModel dictDataModel) {
        DictData dictData = new DictData();
        // dictTypeModel 对象拷贝成 dictType
        BeanUtils.copy(dictDataModel, dictData);
        dictData.setUpdateTime(new Date());
        int i = dictDataMapper.updateByPrimaryKeySelective(dictData);
        if (i > 0) {
            return CommonResult.ok(EmErrorCode.UPDATE_OK);
        } else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
    /**
    * @Author Root
    * @Description 分页查询
    * @Date 2023/7/24 19:27
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param pageRequest
    * @param dictId
    */
    @Override
    public CommonResult selectById(PageRequest pageRequest, Integer dictId) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(dictDataMapper.selectById(pageRequest,dictId));
        return CommonResult.ok(pageResult);
    }
    /**
    * @Author Root
    * @Description 添加字典用户信息
    * @Date 2023/7/24 19:28
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param dictDataModel
    */
    @Override
    public CommonResult insertDictData(DictDataModel dictDataModel) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(dictDataModel)){
            DictData dictData = new DictData();
            // dictDataModel 对象拷贝成 dictData
            BeanUtils.copy(dictDataModel, dictData);
            dictData.setCreateTime(new Date());
            dictData.setUpdateTime(new Date());
            int i = dictDataMapper.insertSelective(dictData);
            if(i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }
    /*
    * @Author Root
    * @Description 删除字典信息
    * @Date 2023/7/24 19:28
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param dataId
    */
    @Override
    public CommonResult deleteDictData(Integer dataId) throws CommonException {
        if(ObjectUtils.isNotEmpty(dataId)){
            int i = dictDataMapper.deleteByPrimaryKey(dataId);
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else {
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }
    /*
    * @Author Root
    * @Description 模糊查询
    * @Date 2023/7/24 19:28 
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param dictLabel
    */
    public CommonResult selectLike(String dictLabel) {
        Example example = new Example(DictData.class);
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(dictLabel)){
            example.createCriteria().andLike("dictLabel","%"+dictLabel+"%");
        }
        return CommonResult.ok(dictDataMapper.selectByExample(example));
    }

    @Override
    public CommonResult selectDict(DictDataModel dictDataModel) {
        List<DictData> dictData;

        // 尝试从 Redis 缓存中获取 dictData

            // 如果缓存中没有 dictData，则从数据库中获取它
            dictData = dictDataMapper.selectDict(dictDataModel);

            // 打印获取到的 dictData
            System.out.println(dictData);

            // 将获取到的 dictData 存储到 Redis 缓存中
            boolean cacheSetSuccess = RedisUtil.set("dictData", dictData);

            if (cacheSetSuccess) {
                // 如果成功将 dictData 缓存起来，返回它
                Object recachedDictData = RedisUtil.get("dictData");
                dictData = (List<DictData>) recachedDictData;
                return CommonResult.ok(dictData);
            } else {
                // 如果缓存操作失败，返回错误响应
                return CommonResult.error(500);
            }
    }


    /** 
     * 
     * @author 温
     * @date 2023/7/14 14:25
     * @parameter []
     * @return java.util.List<com.zhuiyun.project.api.dict.entity.DictData>
     */
    @Override
    public List<DictData> selectOneById() {
        List<DictData> dictData1 = dictDataMapper.selectOneById();
        return dictData1;
    }
}
